package com.red.wood.module.ircs.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.module.ircs.dao.IrcsIpInfoDao;

@Repository
public class IrcsIpInfoDaoImpl implements IrcsIpInfoDao {

	private static final Log log = LogFactory.getLog(IrcsIpInfoDaoImpl.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Map<String, Object> getPageData(Map<String, Object> param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		count = listCount(param);
		list = listPageData(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	public List<Map<String, Object>> listPageData(Map<String, Object> param){
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(userLevel,"1") || StringUtils.equals(userLevel, "2")) {
			sql = getSql();
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
		}else {
			sql.append(getSql());
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(" union all ");
			sql.append(getSql());
			sql.append(" and ircs.province != '"+userProvince+"'");
			sql.append(" and ircs.ircsId in (select distinct house.ircsId from t_ircs_houseinfo house where house.HouseProvince = (select provinceid from provinces t where t.province = '"+userProvince+"' limit 1))");
			sql.append(getQueryConf(param));
		}
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		log.info(sql);
		return jdbcTemplate.queryForList(sql.toString());
	}

	public String listCount(Map<String, Object> param) {
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(userLevel, "1") || StringUtils.equals(userLevel, "2")) {
			sql.append(getSqlCount());
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
		}else {
			sql.append("select sum(count) count from (");
			sql.append(getSqlCount());
			sql.append(" and ircs.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(" union all ");
			sql.append(getSqlCount());
			sql.append(" and ircs.province != '"+userProvince+"'");
			sql.append(" and ircs.ircsId in (select distinct house.ircsId from t_ircs_houseinfo house where house.HouseProvince = (select provinceid from provinces t where t.province = '"+userProvince+"' limit 1))");
			sql.append(getQueryConf(param));
			sql.append(") a");
		}
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public StringBuffer getSql() {
		StringBuffer sql = new StringBuffer();
		sql.append("select t.startIp,t.endIp,ircs.ircsName,case when t.type = '0' then '静态' when t.type = '1' then '动态' when t.type = 2 then '保留' end as type,t.sourceUnit,t.allocationUnit,t.allocationTime");
		sql.append(" from t_ircs_ipinfo t inner join t_ircs_ircsdata ircs");
		sql.append(" on t.ircsId = ircs.ircsId");
		sql.append(" where 1 = 1");
		return sql;
	}
	
	public StringBuffer getSqlCount() {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) count");
		sql.append(" from t_ircs_ipinfo t inner join t_ircs_ircsdata ircs");
		sql.append(" on t.ircsId = ircs.ircsId");
		sql.append(" where 1 = 1");
		return sql;
	}
	
	public StringBuffer getQueryConf(Map<String,Object> param) {
		String type = CommonUtils.valueOf(param.get("type"));
		String sourceUnit = CommonUtils.valueOf(param.get("sourceUnit"));
		String allocationUnit = CommonUtils.valueOf(param.get("allocationUnit"));
		String ircsId = CommonUtils.valueOf(param.get("ircsId"));
		StringBuffer queryConf = new StringBuffer();
		
		if(StringUtils.isNotBlank(ircsId)) {
			queryConf.append(" and t.ircsId='"+ircsId+"'");
		}
		if(!StringUtils.equals(type, "all")){
			queryConf.append(" and t.type='"+type+"'");
		}
		if(StringUtils.isNotBlank(sourceUnit)) {
			queryConf.append(" and t.sourceUnit like '%"+sourceUnit+"%'");
		}
		if(StringUtils.isNotBlank(allocationUnit)) {
			queryConf.append(" and t.allocationUnit like '%"+allocationUnit+"%'");
		}
		return queryConf;
	}
}
